function creat (cr) {
    return document.createElement(cr);
}


// 画网格
var net = document.getElementById("net");
for (var i = 0;i < 10;i++) {
    for (var j = 0;j < 10;j++) {
        var ndiv = document.createElement("div");     
        if (j == 9) {ndiv.style.borderRight = "1px solid black";}
        if (i == 9) {ndiv.style.borderBottom = "1px solid black";}
        net.appendChild(ndiv);
    }
}

// 画方格，装棋子
var grid = document.getElementById("grid");
for (var i = 0;i< 11;i++) {
    var gdiv = creat("div");
    for (var j = 0;j<11;j++) {
        var gdivs = creat("div");
        gdivs.className = "chess";
        gdiv.appendChild(gdivs);
    }
    grid.appendChild(gdiv);
}

// console.log(gdivs.getAttribute("x"));

var count = 0;
var arr = [];
var wc = 0;
var bc = 0;
// 计时
var t1 = 0;
var t2 = 0;
var t = 0;

// 加棋子
var chess = document.getElementsByClassName("chess");
for (var i = 0;i< chess.length;i++) {
    (function (i) {
        chess[i].onclick = function () {
            if (t1 == 0) {   //计时开始
                var myDate = new Date();
                t1 = myDate.getTime();
            }  
            if (chess[i].style.backgroundImage == "") {
                
                if (count % 2 == 0) {
                    chess[i].style.backgroundImage = "url(b.png)";                    
                }else {
                    chess[i].style.backgroundImage = "url(w.png)";                   
                }
                var obj = chess[i];                  
                    arr[count] = i; //记录下棋过程，悔棋功能基础
                    bb(obj); //用来判断输赢
                count++;        //用来计算步数

                ee();
            }           
        }
    }(i))
}


// 判胜负
function bb(obj) { 
    var num = arr[count];
    var win = chess[num].style.backgroundImage;

    // 此处调用四个函数，用来判断四个方向是否五子连星
    
    if (heng(num,win)+shu(num,win)+pie(num,win)+na(num,win) > 0) {
        dd();
    }
    // heng(num,win);
    shu(num,win);
    // pie(num,win);
    // na(num,win);
    // if (heng(num,win)||shu(num,win)||pie(num,win)||na(num,win)) {
    //     dd();
    // }
}
function heng(num,win) {
    for (var i = num;i < 121;i++) {
        if(parseInt(i/11) != parseInt(num/11)) {
            break;
        }
        if (win != chess[i].style.backgroundImage) {
            break;
        }
    }
    for (var j = num;j >= 0;j--) {
        if(parseInt(j/11) !=parseInt(num/11)) {
            break;
        }
        if (win != chess[j].style.backgroundImage) {
            break;
        }
    }
    console.log(i-j-1);
    if(i-j-1 >= 5) {
        return 1
    }else {
        return 0;
    }
}
function shu(num,win) {
    for (var i = num;i < 121;i+=11) {
        if (win != chess[i].style.backgroundImage) {
            break;
        }
    }
    for (var j = num;j >= 0;j-=11) {
        if (win != chess[j].style.backgroundImage) {
            break;
        }
    }
    // console.log((i-j)/11-1);
    if((i-j)/11-1 >= 5) {
        return 1
    }else {
        return 0;
    }
}
function pie(num,win) {
    for (var i = num;i < 121;i+=10) {        
        if (win != chess[i].style.backgroundImage) {
            break;
        }
    }
    for (var j = num;j >= 0;j-=10) {       
        if (win != chess[j].style.backgroundImage) {
            break;
        }
    }
    // console.log((i-j)/10 - 1);
    if((i-j)/10-1 >= 5) {
        return 1
    }else {
        return 0;
    }
}
function na(num,win) {
    for (var i = num;i < 121;i+=12) {        
        if (win != chess[i].style.backgroundImage) {
            break;
        }
    }
    for (var j = num;j >= 0;j-=12) {       
        if (win != chess[j].style.backgroundImage) {
            break;
        }
    }
    if((i-j)/12-1 >= 5) {
        return 1
    }else {
        return 0;
    }
}


//此处绑定一个事件，做悔棋功能
var repent = document.getElementById("repent");
repent.onclick = function cc() {
    if (count > 0) {
        count--;    
        chess[arr[count]].style.backgroundImage = "";
        arr.splice(count);

        ee();
    }  
}


// 赢了之后干的事
var ending = document.getElementById("ending");
var shade = document.getElementById("shade");
var endps = ending.getElementsByTagName("p");
function dd () {
    //让悔棋的函数变成一个空函数，意思是赢了后就不能悔棋了的意思
    repent.onclick = function cc() {}  
    //同上，赢棋后不能认输 
    de.onclick = function ff() {}  
    ending.style.display = "block";
    shade.style.display = "block";   
    count++;
    wc = parseInt(count/2);
    bc = count-wc;

    if (count%2) {endps[0].innerText = "黑方胜!";
    }else {endps[0].innerText = "白方胜!";}

    endps[1].innerText = "总步数："+count;
    endps[2].innerText = "黑方步数："+bc;
    endps[3].innerText = "白方步数："+wc;

    var endDate = new Date();    //Game over;
    t2 = endDate.getTime();
    
    t3 = (t2-t1)/1000;
    endps[4].innerText = "本局用时："+parseInt(t3)+"s";

}


// 判断黑白回合 fn ee;
var tips = document.getElementById("tips");
function ee() {
    if (count%2) {
        tips.innerText = "白子回合";
        tips.style.color = "#fff";
    }else {
        tips.innerText = "黑子回合";
        tips.style.color = "#333";
    }
}

// 认输功能
var de = document.getElementById("de");
var del = document.getElementById("del");
de.onclick = function ff() {
    shade.style.display = "block";
    del.style.display = "block";
    if (count%2) {
        // "白子回合";
        del.getElementsByTagName("p")[0].innerText = "白方认输";
    }else {
        // "黑子回合";
        del.getElementsByTagName("p")[0].innerText = "黑方认输";
    }
}


